package com.stormrage.edu.serv.learn.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.learn.LeaStudentDao;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.edu.serv.student.StuInfoServ;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.LeaTerm;
import com.stormrage.entity.learn.req.LeaApplyReq;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormrage.entity.student.StuInfo;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: LeaStudentServImpl.java
 * @Package com.stormrage.edu.serv.learn.impl
 * @Description: 学员 Service
 * @author www.stormragetech.com
 * @date 2016年4月15日
 * @version V1.0
 */
@Service
public class LeaStudentServImpl implements LeaStudentServ {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudentServImpl.class);

	@Autowired
	private LeaStudentDao leaStudentDao;

	@Autowired
	private LeaTermServ leaTermServ;

	@Autowired
	private StuInfoServ stuInfoServ;

	@Override
	public Integer create(final LeaStudent arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentServ 】- 【 Create 】- 开始运行 ");

		Integer index = leaStudentDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 LeaStudentServ 】- 【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public Integer create(final LeaApplyReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 LeaStudentServ 】- 【 Create 】- 开始运行 ");

		// ------------------ 验证用户是否可以报名 ------------------
		StuInfo info = stuInfoServ.queryByAcct(arg0.getAcctId());

		if (null == info)
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "尚未完善个人资料，请在个人模块进行修改",
					StormrageTechException.ERR_APP, "【 LeaStudentServ 】- 【 Create 】");

		// if (null == acct.getAvatar() || "".equals(acct.getAvatar()) ||
		// acct.getAvatar().contains("avatar"))
		// throw StormrageTechException.instance(ErrCons.SERV_ERR,
		// "尚未上传头像，请在个人模块进行修改", StormrageTechException.ERR_APP,
		// "【 LeaStudentServ 】- 【 Create 】");

		// ------------------ 获取报名班级 ------------------
		LeaTerm term = leaTermServ.queryById(arg0.getTermId());

		// ------------------ 验证用户是否已经报名 ------------------
		LeaStudentReq stuReq = LeaStudentReq.instance(arg0.getAcctId(), term.getId(), null);
		LeaStudent stu = queryByAcctTerm(stuReq);
		logger.debug(" 是否报名 " + stuReq + "结果" + stu);
		if (null != stu && stu.getLocked().equals(0))
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "您已报名此专业，请进入学习模块开始学习",
					StormrageTechException.ERR_APP, "【 LeaStudentServ 】- 【 Create 】");
		if (null != stu && stu.getLocked().equals(1))
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "您已报名此专业，请缴费并等待管理员审核",
					StormrageTechException.ERR_APP, "【 LeaStudentServ 】- 【 Create 】");

		// ------------------ 报名 ------------------
		stu = LeaStudent.instance(arg0.getAcctId(), term.getBunch(), new Date(), 1);
		Integer index = leaStudentDao.create(stu);

		logger.debug(" 【 Serv 】- 【 LeaStudentServ 】- 【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public LeaStudent queryByAcctBunch(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 queryByAcctMajor 】- 【 queryByAcctBunch 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0.getAcctId());
		map.put("bunchId", arg0.getBunchId());

		LeaStudent entity = leaStudentDao.queryByAcctBunch(map);

		logger.debug(" 【 Serv 】- 【 queryByAcctMajor 】- 【 queryByAcctBunch 】- 完成运行 ");
		return entity;

	}

	@Override
	public LeaStudent queryByAcctMajor(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 queryByAcctMajor 】- 【 queryByAcctMajor 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0.getAcctId());
		map.put("majorId", arg0.getMajorId());

		LeaStudent entity = leaStudentDao.queryByAcctMajor(map);

		logger.debug(" 【 Serv 】- 【 queryByAcctMajor 】- 【 queryByAcctMajor 】- 完成运行 ");
		return entity;

	}

	@Override
	public LeaStudent queryByAcctTerm(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 queryByAcctMajor 】- 【 queryByAcctTerm 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0.getAcctId());
		map.put("termId", arg0.getTermId());

		LeaStudent entity = leaStudentDao.queryByAcctTerm(map);

		logger.debug(" 【 Serv 】- 【 queryByAcctMajor 】- 【 queryByAcctTerm 】- 完成运行 ");
		return entity;

	}

}
